package org.dei.perla.rockfall.gui.queryforge;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FilterFileParser {
	
	public static int[] parse(BufferedReader reader) throws FilterFileParserException{
		int []readTaps = null;
		
		try {
			int tapsNum = 1;
			String readLine;
			do{
				readLine = reader.readLine(); //skippo la riga
				if(readLine == null)
					throw new FilterFileParserException("File corrotto, impossibile continuare");
			} while(!readLine.contains("number of taps in decimal"));
			
			String splitted[] = readLine.split("[ ]+");
			
			for(String splittedElement: splitted ){
				if(!splittedElement.equals("")){
					tapsNum = Integer.parseInt(splittedElement);
					break;
				}
			}
			
			readTaps = new int[tapsNum];
			
			do{
				readLine = reader.readLine(); //skippo la riga
				if(readLine == null)
					throw new FilterFileParserException("File corrotto, impossibile continuare");
			} while(!readLine.contains("coefficient of tap"));
			
			splitted = readLine.split("[ ]+");
			
			for(String splittedElement: splitted ){
				if(!splittedElement.equals("")){
					readTaps[0] = Integer.parseInt(splittedElement);
					break;
				}
			}
			
			for(int i = 1; i < tapsNum; i++){
				readLine = reader.readLine(); //riga col numero di taps
				if(readLine == null)
					throw new FilterFileParserException("File corrotto, impossibile continuare");
				splitted = readLine.split("[ ]+");
				
				for(String splittedElement: splitted ){
					if(!splittedElement.equals("")){
						readTaps[i] = Integer.parseInt(splittedElement);
						break;
					}
				}
			}			
		} catch (IOException e) {
			e.printStackTrace();
			throw new FilterFileParserException("File corrotto, impossibile continuare");
		}
		
		return readTaps;
		
	}
	/*public static void main(String args[]){
		if(args.length == 1){
			try {
				FileReader fileReader;
				File input = new File(args[0]);
				fileReader = new FileReader(input);
				BufferedReader bufferedReader = new BufferedReader(fileReader);
				int taps[] = FilterFileParser.parse(bufferedReader);
				for(int tap: taps){
					System.out.println(tap);
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FilterFileParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}*/
}
